package com.rudolfheszele.smsselector.model;

import android.database.Cursor;
import android.telephony.SmsMessage;

public class SmsSelectorSmsMessage
{
	public static final String ID_COLUMN_NAME 					= "_id";
	public static final String THREAD_ID_COLUMN_NAME			= "thread_id";
	public static final String ADDRESS_COLUMN_NAME 				= "address";
	public static final String PERSON_COLUMN_NAME 				= "person";
	public static final String DATE_COLUMN_NAME					= "date";
	public static final String PROTOCOL_COLUMN_NAME				= "protocol";
	public static final String READ_COLUMN_NAME					= "read";
	public static final String STATUS_COLUMN_NAME 				= "status";
	public static final String TYPE_COLUMN_NAME 				= "type";
	public static final String REPLY_PATH_PRESENT_COLUMN_NAME	= "reply_path_present";
	public static final String SUBJECT_COLUMN_NAME	 			= "subject";
	public static final String BODY_COLUMN_NAME 				= "body";
	public static final String SERVICE_CENTER_COLUMN_NAME		= "service_center";
	
	public enum Type
	{
		Received,
		Sent,
		Draft,
		Invalid
	}
	
	/**
	 * 
	 * @param cursor The cursor where the messages can be queried from. Moving of the cursor is not this method`s responsible
	 * @param folder The folder where the messages located
	 * @return The SmsMessage itself
	 */
	public static SmsSelectorSmsMessage fetchMessageFromCursor(Cursor cursor, String folder)
	{
		SmsSelectorSmsMessage message  = new SmsSelectorSmsMessage();
		
		message.mMessageId = cursor.getLong(cursor.getColumnIndex(SmsSelectorSmsMessage.ID_COLUMN_NAME));
		message.mThreadId = cursor.getLong(cursor.getColumnIndex(SmsSelectorSmsMessage.THREAD_ID_COLUMN_NAME));
		message.mAddress = cursor.getString(cursor.getColumnIndex(SmsSelectorSmsMessage.ADDRESS_COLUMN_NAME));
		message.mPersonId = cursor.getLong(cursor.getColumnIndex(SmsSelectorSmsMessage.PERSON_COLUMN_NAME));
		message.mDate = cursor.getLong(cursor.getColumnIndex(SmsSelectorSmsMessage.DATE_COLUMN_NAME));
		if(cursor.getInt(cursor.getColumnIndex(SmsSelectorSmsMessage.READ_COLUMN_NAME)) == 0)
			message.mRead = false;
		else
			message.mRead = true;
		switch(cursor.getInt(cursor.getColumnIndex(SmsSelectorSmsMessage.TYPE_COLUMN_NAME)))
		{
			case 1:
				message.mType = Type.Received;
				break;
			case 2:
				message.mType = Type.Sent;
				break;
			case 3:
				message.mType = Type.Draft;
				break;
			default:
				message.mType = Type.Invalid;
				break;
		}
		message.mBody = cursor.getString(cursor.getColumnIndex(SmsSelectorSmsMessage.BODY_COLUMN_NAME));
		message.mOriginalFolder = folder.toString();
		message.mLogicalFolder = folder.toString();
		
		return message;
	}
	
	public static SmsSelectorSmsMessage createSmsMessage(SmsMessage message)
	{
		SmsSelectorSmsMessage smsMessage = new SmsSelectorSmsMessage();
		
		smsMessage.mMessageId = -1;
		smsMessage.mThreadId = -1;
		smsMessage.mAddress = message.getOriginatingAddress();
		smsMessage.mPersonId = -1;
		smsMessage.mDate = message.getTimestampMillis();
		smsMessage.mRead = false;
		smsMessage.mType = Type.Received;
		smsMessage.mBody = message.getMessageBody();
		smsMessage.mOriginalFolder = "inbox";
		smsMessage.mLogicalFolder = "inbox";
		
		return smsMessage;
	}
	
	// Getter methods
	public long getMessageId()
	{
		return mMessageId;
	}
	
	public long getThreadId()
	{
		return mThreadId;
	}
	
	public String getAddress()
	{
		return mAddress;
	}
	
	public long getPersonId()
	{
		return mPersonId;
	}
	
	public String getPersonName()
	{
		return mPersonName;
	}
	
	public long getDate()
	{
		return mDate;
	}
	
	public boolean isRead()
	{
		return mRead;
	}
	
	public Type getType()
	{
		return mType;
	}
	
	public String getBody()
	{
		return mBody;
	}
	
	public String getOriginalFolder()
	{
		return mOriginalFolder;
	}
	
	public String getLogicalFolder()
	{
		return mLogicalFolder;
	}
	
	public SmsSelectorRuleSet getAppliedRuleSet()
	{
		return mAppliedRuleSet;
	}
	
	// Setter methods
	
	public void setPersonName(String name)
	{
		mPersonName = name;
	}
	
	void setLogicalFolder(String logicalFolder)
	{
		mLogicalFolder = logicalFolder;
	}
	
	void deleteMessage()
	{
		mLogicalFolder = "Deleted";
	}
	
	void setAppliedRuleSet(SmsSelectorRuleSet ruleSet)
	{
		mAppliedRuleSet = ruleSet;
	}
	
	// Android database related data
	private long mMessageId;
	private long mThreadId;
	private String mAddress;
	private long mPersonId;
	private String mPersonName;
	private long mDate;
	private boolean mRead;
	private Type mType;
	private String mBody;
	
	// Application related data
	private String mOriginalFolder;
	private String mLogicalFolder;
	private SmsSelectorRuleSet mAppliedRuleSet;
}
